package VacationHomework.LeetCode;

public class Num142 {
    public ListNode detectCycle(ListNode head) {
        if(head==null || head.next==null){
                    return null;
                }
                ListNode slow=head,fast=head;
                while (fast!=null && fast.next!=null){
                    slow=slow.next;
                    fast=fast.next.next;
                    if (slow==fast){
                        ListNode third=head;
                while (third!=slow){
                    slow=slow.next;
                    third=third.next;
                }
                return slow;
            }
        }
       return null;
    }
}
